<?php
/**
 * Thread Model
 * 
 * @package  : CodeIgniter
 * @category : models
 * @author   : Oneplus <oneplus.lau@gmail.com>
 * @data     : 2011-06-13
 */
class Thread extends CI_Model {
	function __construct() {
		parent::__construct();
		$this->load->model('user');
		$this->load->database();
	}
	
	public function createThread($data) {
		$this->db->insert('threads', $data);
		return $this->db->insert_id();
	}
	
	public function deleteThread($tid) {
		$this->db->delete('threads', array('tid' => $tid));
	}
	
	public function getThread($tid) {
		$query = $this->db->get_where('threads', array('tid' => $tid));
		$uid = $query->row()->uid;
		$user = $this->user->getUser($uid);
		$thread = array(
			'tid'         => $tid,
			'title'       => $query->row()->title,
			'content'     => $query->row()->content,
			'create_time' => $query->row()->create_time,
			'uid'         => $uid,
			'username'    => $user['username']);
			
		return $thread;
	}
	
	public function touch($tid) {
		$last_reply = date("Y-m-d H:i:s", time());
		$this->db->update('threads', array('last_reply' => $last_reply));
	}
}
?>